import flask
from flask import Blueprint
import db

page_login = Blueprint('page_login', __name__)


@page_login.route('/login', methods=['GET', 'POST'])
def login():
    """
    login page
    :return:
        when method is GET, just return HTML page
        when method is POST, get id and password form front end and query the db to check if they match
        after validation, return the result in strings
    """
    if flask.request.method == 'POST':
        name = flask.request.form.get('name')
        psw = flask.request.form.get('psw')
        if name is None or psw is None:
            return flask.make_response('Failed', 403)  # error
        check_res, uid, authority = db.login_check(name, psw)
        if check_res == 0:
            return 'success' + str(uid)
        else:
            return 'fail'
    if flask.request.method == 'GET':
        return flask.render_template('./static/templates/login.html')